GBML manual / Collision

Collision layers

How to make a door that opens, a bridge that appears or a wall that falls down - draw each state of the room as a layer in the Room Editor, then switch between the layers from your code.

The idea

Most of a room never changes. The floor is floor and the walls are walls. But some things in a game change while you play. A locked door opens when you find the key. A bridge appears when you pull a lever. A cracked wall falls down when something blows up.

A collision layer is how you build those changes. You do not write any code to say what changes. You draw it.

Your code then says one thing: switch to layer 2. The game does the rest. The door cells stop blocking, and they show the open door.

One layer is on at a time

A room is always on exactly one layer. When you switch, the room becomes layer 1 with that layer's changes, and nothing else. Layers never stack on top of each other.

Think of the layers as the different states of the room. Layer 1 is "door shut". Layer 2 is "door open". If you also have a bridge that can appear, then a room with both a door and a bridge has these states:

You draw the states the player can reach, and your code switches between them.

Layers are numbered

Your code picks a layer by its number. In the Room Editor a new layer goes on the end of the list, so it never changes the number of a layer you already have. You cannot move a layer or put one in the middle. Layer 3 stays layer 3.

The one thing that changes numbers is deleting a layer. Delete layer 3 and every layer after it moves down by one: layer 4 becomes layer 3, layer 5 becomes layer 4, and so on. Layer 1 is the room the way you first drew it, so it can never be deleted. See Delete a layer, below, for what the editor does about your code.

The name you type helps you remember what a layer is for. Out of the box it is just for you, and your code never uses it. If you would rather write west_room_door_open than 3, you can turn that on: see Use a layer's name instead of its number, below. The numbers still work either way.

Make a layer in the Room Editor

  1. Open the room and find Collision Layers in the Collision section of the left panel. It shows which layer you are looking at. Right now it says Layer 1 of 1.
  2. Press + new layer. You are now on layer 2. It looks just like layer 1 until you paint something different.
  3. Type a name in the Name box. Use letters, numbers and _, and start with a letter. door_open works. (If you turn on layer names for your code, this name becomes part of a word you write. The box then says Name (used in your code).)
  4. Paint what the room looks like with the door open. Paint floor tiles over the door, and mark those cells as passable.
  5. Use the left and right arrows to flip between layers and check your work. Layer 1 should still show the shut door.

The box under the number tells you the call to use. On layer 2 it says tile_layer_set_active(2). With layer names turned on, it shows the word instead, like tile_layer_set_active(west_room_door_open).

A layer only remembers what is different from layer 1. If you change layer 1 later, layers 2 and up change along with it, except for the tiles they changed themselves.

Copy current layer makes a new layer that starts with the changes of the one you are looking at. It is handy when two layers are almost the same.

Delete a layer

Look at the layer you want to remove and press delete layer. Layer 1 cannot be deleted. The button is switched off there.

Because the layers after it move down by one, a number in your code can end up meaning a different layer. So the editor looks through your code first, and it only speaks up if something there is affected:

A saved game stores which layer a room was on, as a number. If a player already saved while standing on a layer above the deleted one, their save now points at a different layer. That save lives on their own cartridge, so nothing in the editor can change it. The editor says so, but only when your code can switch layers at all.

If you want a layer to stop doing anything without moving any numbers, choose Empty it instead in that question. The layer stays where it is, but it looks just like layer 1 again.

One Undo puts the layer back, and the numbers in your code with it.

Switch layers from your code

A key lies on the floor. When the hero touches it, the door opens for good:

// obj_key, Collision event with obj_hero: the hero picked the key up.
tile_layer_set_active(2);   // layer 2 is "door open"
save_commit();
instance_destroy(self);

Three lines do the whole job. tile_layer_set_active switches the room to layer 2. The door cells stop blocking and show the open door. save_commit saves the game, so the door is still open the next time the cartridge is switched on. The key removes itself, so it cannot be picked up twice.

To go back, switch to layer 1. To ask which layer is on right now, use tile_layer_active. Together they make a door that toggles:

// Press the interact button to open or shut the door.
if (action_pressed(interact)) {
    if (tile_layer_active() == 1) {
        tile_layer_set_active(2);
    } else {
        tile_layer_set_active(1);
    }
}

Use a layer's name instead of its number

Numbers are easy to mix up. Type tile_layer_set_active(3) when you meant 2, and the game does not complain. It just opens the wrong door. A name is harder to get wrong, because a name you spell wrong stops the build.

So you can ask for names. Open Project Settings, find Collision layers, and tick Use layer names in my code. It is one setting for the whole game. It is not a setting for each layer, because a game picks its layers either by name or by number.

Now every layer from 2 up has a word. The word is the room's name, then _, then the layer's name. Layer 3 of the room west_room, called door_open, is the word west_room_door_open:

tile_layer_set_active(west_room_door_open);    // exactly the same as tile_layer_set_active(3)

Here it is in a real object. A lever opens the bridge in the west room:

// obj_lever, Step event: pull the lever (interact) to put the bridge down.
if (action_pressed(interact)) {
    tile_layer_set_active(room_doc_west_bridge);   // layer 2 of room_doc_west
    save_commit();
}

The word is only a number in disguise. The build swaps it for the real number before anything runs, so the game never sees the name. Nothing is looked up while you play, and it costs nothing. A word works anywhere a number does, like if (tile_layer_active() == west_room_door_open). You cannot put a new value into it, because it is a layer number and not a variable.

Why the room's name comes first. Two rooms can both have a layer called door_open. The words west_room_door_open and east_room_door_open are different, so they can never be mixed up. A layer name only has to be different from the other layers in its own room.

Layer 1 has no word. It has no name, because it is the room as you first drew it. For layer 1 you always write the number 1.

One thing a word cannot know. tile_layer_set_active still works on the room the player is in right now. east_room_door_open is just the number 2. If you use it while the player is in west_room, it switches west_room to its layer 2. The build cannot see which room the player will be in, so keep each word in the objects that live in that room.

Names that cannot work

When you tick the box, every layer name becomes a word in your code, so each one is checked like any other name. A word can only use letters, numbers and _, and cannot start with a number. It also cannot be a name that something else already has: a built-in command, one of your objects, scripts, #macros, enums or variables, or an input action.

What gets checked is the whole word, not just the layer name. A layer called door_open is fine on its own. But in a room called my room, the word is my room_door_open, and that has a space in it. So the room's name can be the part that needs fixing.

One clash can still happen. A room a with a layer b_c, and a room a_b with a layer c, both make the word a_b_c. The build stops and names both layers. Neither gets the word, so it never means the wrong one.

When you tick the box, Project Settings lists every name that cannot work, with a button for each that takes you to the layer or the room name. You can still save while some are on the list. The Validate list and the build keep telling you until they are fixed, or until you untick the box.

When a name changes

Names are checked every time you build, so a change cannot sneak past you. That is the big win over numbers. Each of these stops the build with a message that says what happened:

If you rename a room

A room that has layers is easy to rename by accident, so here is everything a rename does:

  1. If you use layer names, every layer word in that room changes. The build catches this, and the editor offers to change the words in your code for you.
  2. Saved games are not affected. Every room gets a number of its own when you make it. The number never changes when you rename the room or move it in the list, and it is never given to another room, even after the room that had it is deleted. A saved game remembers a room's layer under that number, so a door the player opened is still open after you rename the room.

A copy of a room (Duplicate) is a new room, so it gets a number of its own and does not share the original's saved layer.

What the game does for you

It remembers. Open the door, leave the room, come back. The door is still open. Every time a room loads, the game puts it back on the layer it was on before anything else in the room wakes up. Your objects can look at the door in their Create event and already see it open.

It writes both halves. A layer changes what a tile blocks and what it looks like. Both change together, so you never end up with a door that looks open but still stops the hero.

Each room has its own layers. Two rooms can both have a layer 2. Switching one does not touch the other. The calls always work on the room the player is in right now.

A number that is not there

A layer number the room does not have does nothing at all, and that is exactly how a mistake hides: the door just never opens.

So the build helps. If you write the number out, like tile_layer_set_active(5), the build checks that some room in your game has that many layers. If none does, the build stops and tells you the most any room has. The build cannot check a number that is made while the game runs, and it cannot tell if you chose the wrong room's layer, only that the number exists somewhere.

Layer names do better. A word like west_room_door_open is only there while that layer is. If the layer is gone or renamed, the word is gone, and the build stops. A wrong number can hide. A wrong word cannot.

What happens to changes you make in code

tile_set_class, tile_set_flags and tile_set_index change one tile at a time. If you change a tile that way and then switch to a different layer, your change is lost. Switching lays the whole room down again, so every tile goes back to what the new layer says. Switching to the layer that is already on does nothing, and your changes stay.

Limits

Layers or tile_set_class?

tile_set_class and tile_set_index change one tile at a time, in code, and the change is forgotten when the room is left. They are the right tool for something that happens all the time, like digging. A collision layer is the right tool for a change you designed: you drew it, you numbered it, and the game remembers it.

See also: tile_layer_set_activetile_layer_activetile_set_classtile_set_indexTile flagsOverlap and collisionProject settings